package co.fitstart.coach.web.controller;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.handler.CookieHandler;
import com.demo2do.core.web.interceptor.WechatWebSniffer;

import co.fitstart.coach.entity.security.Login;
import co.fitstart.coach.web.interceptor.AutoLogin;
import co.fitstart.coach.web.resolver.Secure;

@Controller
@RequestMapping("")
public class LogoutController {
    
    private static final Logger logger = LoggerFactory.getLogger(LogoutController.class);
    
    @Value("#{weixin['userid.key']}")
    private String useridKey;
    
    @Autowired
    private CookieHandler cookieHandler;
    
    /**
     * 
     * @param login
     * @param response
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @AutoLogin(ignore = true)
    public @ResponseBody String onLogout(@Secure Login login, HttpServletResponse response) {
        
        Result result = new Result();
        
        try {
            
            cookieHandler.removeCookie(response, useridKey);
            
            result.success().message("退出登录成功");
                
            
        } catch (Exception e) {
            
            logger.error("LogoutController#onLogout() error", e);
            result.fail().message("退出登录失败");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/logout-message", method = RequestMethod.GET)
    @WechatWebSniffer(ignore = true)
    @AutoLogin(ignore = true)
    public String logout() {
        return "logout";
    }

}
